package com.tg.app.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;


import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.TimeZoneAdapter;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tg.data.http.entity.TimeZoneBean;

import com.tg.app.view.SetSuccessMsgToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TimeZoneActivity extends BaseActivity {


    private TimeZoneAdapter mAdapter;
    private List<TimeZoneBean> mData = new ArrayList<>();
    private DeviceSettingsInfo info;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_timezone);
        hideActionBar();
        initView();
        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        getTimeZone();
    }

    @Override
    protected void initView() {
        RecyclerView mRecycleview = findViewById(R.id.recycleview);
        mRecycleview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TimeZoneAdapter(this, mData);
        mAdapter.setOnItemClickListener(new TimeZoneAdapter.OnItemClickListener() {
            @Override
            public void onClick(int positon) {
                setTimezone(positon);
            }
        });
        mRecycleview.setAdapter(mAdapter);
        findViewById(R.id.back_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView tvTitle = findViewById(R.id.device_name);
        tvTitle.setText(R.string.setting_time_zone);
    }

    private void getTimeZone() {
        showLoading();
        HashMap<String, String> map = new HashMap<>();
        map.put("language", "zh-cn");
        map.put("device_id", String.valueOf(info.deviceID));
        TGHttp.getInstance().getTimeZoneList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<ArrayList<TimeZoneBean>>() {

                    @Override
                    protected void onSuccess(ArrayList<TimeZoneBean> content) {
                        mData.addAll(content);
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {

                    }

                    @Override
                    protected void onOtherError(String error) {

                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }

    private void setTimezone(final int position) {
        showLoading();
        HashMap<String, String> map = new HashMap<>();
        map.put("device_id", String.valueOf(info.deviceID));
        map.put("time", mData.get(position).getTime());
        map.put("zone", mData.get(position).getZone());
        TGHttp.getInstance().setTimeZone(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<String>() {
                    @Override
                    protected void onSuccess(String content) {
                        for (int i = 0; i < mData.size(); i++) {
                            if (position == i) {
                                mData.get(i).setSelected(1);
                            } else {
                                mData.get(i).setSelected(0);
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                        SetSuccessMsgToast.show(TimeZoneActivity.this);
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        showToast(errorInfo);
                    }

                    @Override
                    protected void onOtherError(String error) {

                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }
}
